<template>
  <div>
    <div>
      <div class="header">
        <el-input v-model="id" placeholder="请输入品牌商id"></el-input>
        <el-input v-model="name" placeholder="请输入品牌商名称"></el-input>
        <el-button type="primary" @click="search">
          <svg-icon icon-class="search" />
          查找
        </el-button>
        <el-button type="primary" @click="add">
          <i class="el-icon-edit" />
          添加
        </el-button>
        <el-button type="primary" @click="excelData">
          <svg-icon icon-class="form" />
          导出
        </el-button>
      </div>

      <div class="table">
        <el-table
          border
          :data="tableData"
          style="width: 100%"
          :default-sort="{ prop: 'id', order: 'descending' }"
        >
          <el-table-column prop="id" width="120" label="品牌商ID" sortable>
          </el-table-column>
          <el-table-column prop="name" width="200" label="品牌商名称">
          </el-table-column>
          <el-table-column width="100" label="品牌商图片">
            <template slot-scope="scope">
              <div>
                <img :src="scope.row.picUrl" alt="" />
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="desc" label="介绍"> </el-table-column>
          <el-table-column prop="floorPrice" width="100" label="底价">
          </el-table-column>
          <el-table-column width="200" label="操作">
            <template slot-scope="scope">
              <el-button
                @click="editclick(scope.row)"
                type="primary"
                size="small"
              >
                编辑
              </el-button>
              <el-button
                @click="delclick(scope.row)"
                type="danger"
                size="small"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div style="padding: 40px 0" class="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>

      <div class="popup">
        <el-dialog
          :title="isEdit ? '编辑制造商' : '添加制造商'"
          :visible.sync="dialogFormVisible"
          :before-close="close"
        >
          <el-form :model="form">
            <el-form-item label="品牌商名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="介绍" :label-width="formLabelWidth">
              <el-input v-model="form.desc" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="品牌商图片" :label-width="formLabelWidth">
              <el-upload
                class="avatar-uploader"
                action="http://123.56.162.79:8083/admin/storage/create"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :headers="upheader"
              >
                <img v-if="form.file" :src="form.file" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="底价" :label-width="formLabelWidth">
              <el-input v-model="form.floorPrice" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="subimt">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { getbrand, addbrand, updatebrand, delbrand } from "@/api/mall";
import { getToken } from "@/utils/auth";
import { formatDate } from "@/utils/time";
import { MessageBox, Message } from "element-ui";
export default {
  props: [],
  data() {
    return {
      tableData: [],
      page: 1,
      total: 10,
      limit: 10,
      id: "", //搜索品牌商id
      name: "", //搜索品牌制造商
      dialogFormVisible: false, //是否显示弹出框
      formLabelWidth: "120px",
      form: {
        name: "", //品牌商名称
        floorPrice: "", //底价
        file: "", //图片
        desc: "", //介绍
        addTime: "",
        deleted: false,
        id: "",
        sortOrder: "",
        updateTime: "",
      },
      upurl: "",
      upheader: {
        "X-Mymall-Admin-Token": getToken(),
      },
      isEdit: false,
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    getdata() {
      let params = {
        page: this.page,
        limit: this.limit,
        id: this.id,
        name: this.name,
      };
      getbrand(params).then((res) => {
        console.log(res);
        if (res.errno == 0) {
          this.tableData = res.data.items;
          this.total = res.data.total;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      let str = res.data.url.replace(/localhost:8080/, "123.56.162.79:8082");
      this.form.file = str;

      // this.form.file = URL.createObjectURL(file.raw);
    },
    // 改变每一页多少条数据
    handleSizeChange(e) {
      this.limit = e;
      this.getdata();
    },
    //改变当前页数
    handleCurrentChange(e) {
      this.page = e;
      this.getdata();
    },
    //搜索
    search() {
      this.getdata();
    },
    //点击添加
    add() {
      this.dialogFormVisible = true;
    },
    close() {
      this.form.name = "";
      this.form.floorPrice = "";
      this.form.desc = "";
      this.form.file = "";
      this.isEdit = false;
      this.dialogFormVisible = false;
    },
    //点击取消
    cancel() {
      this.form.name = "";
      this.form.floorPrice = "";
      this.form.desc = "";
      this.form.file = "";
      this.isEdit = false;
      this.dialogFormVisible = false;
    },
    //弹出框确定
    subimt(e) {
      if (this.isEdit == true) {
        this.isEdit = false;
        if (
          this.form.name == "" &&
          this.form.floorPrice == "" &&
          this.form.desc == ""
        ) {
          Message({
            message: "数据不完整",
            type: "error",
            duration: 2 * 1000,
          });
          return;
        }
        let data = {
          addTime: this.form.addTime,
          deleted: this.form.deleted,
          desc: this.form.desc,
          floorPrice: this.form.floorPrice,
          id: this.form.id,
          name: this.form.name,
          picUrl: this.form.file,
          sortOrder: this.form.sortOrder,
          updateTime: this.form.updateTime,
        };
        updatebrand(data).then((res) => {
          console.log(res);
          if (res.errno == 0) {
            this.dialogFormVisible = false;

            this.form.name = "";
            this.form.floorPrice = "";
            this.form.desc = "";
            this.form.file = "";

            this.getdata();
          }
        });
      } else {
        if (
          this.form.name == "" &&
          this.form.floorPrice == "" &&
          this.form.desc == ""
        ) {
          Message({
            message: "数据不完整",
            type: "error",
            duration: 2 * 1000,
          });
          return;
        }
        let data = {
          name: this.form.name,
          desc: this.form.desc,
          floorPrice: this.form.floorPrice,
          picUrl: this.form.file,
        };

        addbrand(data).then((res) => {
          if (res.errno == 0) {
            this.dialogFormVisible = false;
            this.form.name = "";
            this.form.floorPrice = "";
            this.form.desc = "";
            this.form.file = "";
            this.getdata();
          }
        });
      }
    },
    //点击编辑
    editclick(e) {
      console.log(e);
      this.isEdit = true;
      

      this.form.desc = e.desc;
      this.form.floorPrice = e.floorPrice;
      this.form.name = e.name;
      this.form.file = e.picUrl;
      this.form.addTime = e.addTime;
      this.form.deleted = e.deleted;
      this.form.id = e.id;
      this.form.sortOrder = e.sortOrder;
      this.form.updateTime = e.updateTime;
      this.dialogFormVisible = true;
    },
    //点击删除
    delclick(e) {
      let data = {
        addTime: e.addTime,
        deleted: e.deleted,
        desc: e.desc,
        floorPrice: e.floorPrice,
        id: e.id,
        name: e.name,
        picUrl: e.picUrl,
        sortOrder: e.sortOrder,
        updateTime: e.updateTime,
      };
      delbrand(data).then((res) => {
        if (res.errno == 0) {
          Message({
            message: "删除成功",
            type: "success",
            duration: 2 * 1000,
          });
          this.getdata();
        }
      });
    },
    //导出excel表格
    excelData() {
      const headers = {
        品牌商ID: "id",
        品牌商名称: "name",
        品牌商图片: "picUrl",
        介绍: "desc",
        底价: "floorPrice",
      };
      //懒加载
      import("@/vendor/Export2Excel").then((excel) => {
        const data = this.formatJson(headers, this.tableData);
        excel.export_json_to_excel({
          header: Object.keys(headers),
          data,
          filename: "品牌制造商",
        });
      });
    },
    // 该方法负责将数组转化成二维数组
    formatJson(headers, rows) {
      //首先遍历数组
      return rows.map((item) => {
        // item是对象  => 转化成只有值的数组 => 数组值的顺序依赖headers  {username: '张三'  }
        // Object.keys(headers)  => ["姓名", "手机号",...]
        return Object.keys(headers).map((key) => {
          return item[headers[key]]; // 得到 ['张三'，’129‘，’dd‘,'dd']
        });
      });
    },
  },
  computed: {},
};
</script>
<style lang="less" scoped>
.header {
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  /deep/.el-input {
    width: 200px;
    margin-left: 15px;
    .el-input__inner {
      width: 200px;
    }
  }
  .el-button {
    margin-left: 15px;
  }
}
.table {
  padding: 15px;
  /deep/.cell {
    text-align: center;
    img {
      width: 40px;
      height: 40px;
    }
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
